﻿using Aliyun.OSS;
using Infrastructure.Attribute;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZR.DataCollection.ICommunication;
using ZR.DataCollection.InheritanceClass;
using ZR.DataCollection.SingleParameter;

namespace ZR.DataCollection.Communication
{
    public class HandScanCode : BaseHandScanCode
    {
        private string _StationCode;

        public override event Action<string> Notify_Invoke;

        public override event Action<string, string> ScanCode_Notify;

        public HandScanCode(string stationCode)
        {
            try
            {
                _StationCode = stationCode;
                {
                    if (CurrentOPInfo.Current.ScannerConfig.Find(f => f.WorkstationCode.Equals(stationCode)).ScannerType.Equals("1"))
                    {
                        _SerialPort = new SerialPort(CurrentOPInfo.Current.ScannerConfig.Find(f => f.WorkstationCode.Equals(stationCode)).SerialPort.ToLower(), 9600, Parity.None);
                        _SerialPort.ReadTimeout = 1500;
                        _SerialPort.WriteTimeout = 1500;
                        _SerialPort.Open();
                        _SerialPort.DataReceived += SerialPortReceived;
                    }
                    else
                    {
                        _tcpClient = new TcpClient();
                        _tcpClient.Connect("", 100);
                        _thread = new Thread(new ThreadStart(TcpRevice));
                        _thread.IsBackground = true;
                        _thread.Start();
                    }
                }
            }
            catch (Exception EX)
            {
                throw new Exception(EX.Message);
            }
        }

        private void TcpRevice()
        {
            while (true)
            {
                string barCode = string.Empty;
                byte[] data = new byte[1024];
                int len = _tcpClient.Client.Receive(data, data.Length, SocketFlags.None);
                barCode = Encoding.Default.GetString(data, 0, len);
                if (!string.IsNullOrEmpty(barCode))
                {
                    DataRecevied(_StationCode, barCode);
                }
            }
        }

        private void SerialPortReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort serialPort = (SerialPort)sender;
            int bytesToRead = serialPort.BytesToRead;
            byte[] ReDatas = new byte[serialPort.BytesToRead];
            serialPort.Read(ReDatas, 0, bytesToRead);
            var data = Encoding.UTF8.GetString(ReDatas);
            DataRecevied(_StationCode, data);
        }

        /// <summary>
        /// 条码处理
        /// </summary>
        /// <param name="barCode"></param>
        private void DataRecevied(string _StationCode, string barCode)
        {
            Console.WriteLine($"{_StationCode}-{barCode}");
            ScanCode_Notify?.Invoke(_StationCode, barCode);
        }
    }
}